﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DTcms.Common;

namespace DTcms.Web.admin.erp
{
    public partial class erpmanager_edit : DTcms.Web.UI.ManagePage
    {
        private string action = ActionEnum.Add.ToString(); //操作类型
        private int id = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = DTRequest.GetQueryString("action");
            if (!string.IsNullOrEmpty(_action) && _action == ActionEnum.Edit.ToString())
            {
                this.action = ActionEnum.Edit.ToString();//修改类型
                if (!int.TryParse(Request.QueryString["id"] as string, out this.id))
                {
                    JscriptMsg("传输参数不正确！", "back", "Error");
                    return;
                }
                if (!new BLL.dt_erp().Exists(this.id))
                {
                    JscriptMsg("信息不存在或已被删除！", "back", "Error");
                    return;
                }
            }
            if (!Page.IsPostBack)
            {
                //取得管理员信息
                Model.manager model = GetAdminInfo();
                //TreeBind(ddlRoleId, model.role_type);
                if (action == ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(this.id);
                }
            }
        }

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            BLL.dt_erp bll = new BLL.dt_erp();
            Model.dt_erp model = bll.GetModel(_id);
            txtepr_name.Text = model.epr_name;
            txterp_Add.Text = model.erp_Add;
            rblbak1.SelectedValue = model.erp_bak1.ToString();
            txterp_bak3.Text = model.erp_bak3;
            txterp_contacts.Text = model.erp_contacts;
            txterp_email.Text = model.erp_email;
            txterp_number.Text = model.erp_number;
            txterp_phonenumber.Text = model.erp_phonenumber;
            txterp_qq.Text = model.erp_bak4;
            rblsouers.SelectedValue = model.erp_sources.ToString();

            txterp_web.Text = model.erp_web;
            //ddlRoleId.SelectedValue = model.role_id.ToString();
            //rblIsLock.SelectedValue = model.is_lock.ToString();
            //txtUserName.Text = model.user_name;
            //txtUserName.ReadOnly = true;
            //txtUserPwd.Attributes["value"] = txtUserPwd1.Attributes["value"] = model.user_pwd;
            //txtRealName.Text = model.real_name;
            //txtTelephone.Text = model.telephone;
            //txtEmail.Text = model.email;
        }
        #endregion


        #region 增加操作=================================
        private bool DoAdd()
        {

            Model.dt_erp model = new Model.dt_erp();
            BLL.dt_erp bll = new BLL.dt_erp();



            model.epr_name = txtepr_name.Text.Trim();
            model.erp_Add = txterp_Add.Text.Trim();
            model.erp_bak1 = int.Parse(rblbak1.SelectedValue);
            model.erp_bak2 = 0;
            model.erp_bak3 = txterp_bak3.Text.Trim();
            model.erp_bak4 = txterp_qq.Text.Trim();
            model.erp_bak5 = DateTime.Now;
            model.erp_contacts = txterp_contacts.Text.Trim();
            model.erp_email = txterp_email.Text.Trim();
            model.erp_number = txterp_number.Text.Trim();
            model.erp_phonenumber = txterp_phonenumber.Text.Trim();
            model.erp_sources = int.Parse(rblsouers.SelectedValue);
            model.erp_web = txterp_web.Text.Trim();

            model.user_addtime = DateTime.Now;

            Model.manager managermodel = GetAdminInfo();

            model.user_id = managermodel.id;
            model.user_lasttime = DateTime.Now;





            //Model.manager model = new Model.manager();
            //BLL.manager bll = new BLL.manager();
            //model.role_id = int.Parse(ddlRoleId.SelectedValue);
            //model.role_type = new BLL.manager_role().GetModel(model.role_id).role_type;
            //model.is_lock = int.Parse(rblIsLock.SelectedValue);
            //model.user_name = txtUserName.Text.Trim();
            //model.user_pwd = DESEncrypt.Encrypt(txtUserPwd.Text.Trim());
            //model.real_name = txtRealName.Text.Trim();
            //model.telephone = txtTelephone.Text.Trim();
            //model.email = txtEmail.Text.Trim();
            //model.add_time = DateTime.Now;

            return bll.Add(model);
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            bool result = true;
            BLL.dt_erp bll = new BLL.dt_erp();
            Model.dt_erp model = bll.GetModel(_id);

            model.epr_name = txtepr_name.Text.Trim();
            model.erp_Add = txterp_Add.Text.Trim();
            model.erp_bak1 = int.Parse(rblbak1.SelectedValue);
            model.erp_bak3 = txterp_bak3.Text.Trim();
            model.erp_bak4 = txterp_qq.Text.Trim();
            model.erp_contacts = txterp_contacts.Text.Trim();
            model.erp_email = txterp_email.Text.Trim();
            model.erp_number = txterp_number.Text.Trim();
            model.erp_phonenumber = txterp_phonenumber.Text.Trim();
            model.erp_sources = int.Parse(rblsouers.SelectedValue);
            model.erp_web = txterp_web.Text.Trim();

            model.user_lasttime = DateTime.Now;

            //BLL.manager bll = new BLL.manager();
            //Model.manager model = bll.GetModel(_id);

            //model.role_id = int.Parse(ddlRoleId.SelectedValue);
            //model.role_type = new BLL.manager_role().GetModel(model.role_id).role_type;
            //model.is_lock = int.Parse(rblIsLock.SelectedValue);
            //if (txtUserPwd.Text.Trim() != model.user_pwd)
            //    model.user_pwd = DESEncrypt.Encrypt(txtUserPwd.Text.Trim());
            //model.real_name = txtRealName.Text.Trim();
            //model.telephone = txtTelephone.Text.Trim();
            //model.email = txtEmail.Text.Trim();

            if (!bll.Update(model))
            {
                result = false;
            }

            return result;
        }
        #endregion

        //保存
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            BLL.dt_erp bll = new BLL.dt_erp();
            Model.dt_erp model = new Model.dt_erp();
            Model.manager managermodel = GetAdminInfo();

            if (action == ActionEnum.Edit.ToString()) //修改
            {
                ChkAdminLevel("erpmanager", ActionEnum.Edit.ToString()); //检查权限

                model = bll.GetModel(this.id);
                if (model.user_id==managermodel.id)
                {
                    if (!DoEdit(this.id))
                    {
                        JscriptMsg("保存过程中发生错误啦！", "", "Error");
                        return;
                    }
                    JscriptMsg("修改客户资料成功啦！", "erpmanager.aspx", "Success");
                }
                else
                {
                    JscriptMsg("这不是一条属于自己的数据，请不要修改相关内容！", "", "Error");
                    return;
                }

               
            }
            else //添加
            {
                ChkAdminLevel("erpmanager", ActionEnum.Add.ToString()); //检查权限
                string erpname = txtepr_name.Text.Trim();
                string erpphonenuber = txterp_phonenumber.Text.Trim();
                if (bll.GetList("epr_name='" + erpname + "' or erp_phonenumber='"+erpphonenuber+"'").Tables[0].Rows.Count==0)
                {
                    if (!DoAdd())
                    {
                        JscriptMsg("保存过程中发生错误啦！", "", "Error");
                        return;
                    }
                    JscriptMsg("添加客户资料成功啦！", "erpmanager.aspx", "Success");
                }
                else
                {
                    JscriptMsg("ERP系统里面已经存在该条记录，请勿重复添加！", "", "Error");
                    return;
                }

               
            }
        }

    }
}